package com.ken.astroboy.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class CipherUtils {

    public static String encrypt(String plaintext) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException{  
              
        String key = "@aes-privatekey#";    //私钥： AES固定格式为128/192/256 bits.即：16/24/32bytes。DES固定格式为128bits，即8bytes。  

        String iv   = "aa11cc22ee33gg44";   //初始化向量参数，AES 为16bytes. DES 为8bytes.  

        Key keySpec = new SecretKeySpec(key.getBytes(), "AES");    //两个参数，第一个为私钥字节数组， 第二个为加密方式 AES或者DES  
      
        IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes());  // 初始化向量    
      
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");   // 实例化加密类，参数为加密方式，要写全  

        cipher.init(Cipher.ENCRYPT_MODE,  keySpec, ivSpec); // 初始化，此方法可以采用三种方式，按服务器要求来添加。  
                                                                      //（1）无第三个参数  
                                                                      //（2）第三个参数为SecureRandom random = new SecureRandom();中random对象，随机数。(AES不可采用这种方法)  
                                                                      //（3）采用此代码中的IVParameterSpec  
      
        //cipher.init(Cipher.ENCRYPT_MODE, keySpec);  
      
        //SecureRandom random = new SecureRandom();  
      
        //cipher.init(Cipher.ENCRYPT_MODE, keySpec, random);  
      
        byte [] b = cipher.doFinal(plaintext.getBytes());//加密操作,返回加密后的字节数组，然后需要编码。主要编解码方式有Base64, HEX, UUE,7bit等等。  

        String ret = Base64.getEncoder().encodeToString(b);   //Base64、HEX等编解码  
      
        return ret;
    }  
    
    public static String decrypt(String ciphertext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{  
        
        String keySpec = "@aes-privatekey#";  
                
        byte [] b = Base64.getDecoder().decode(ciphertext);         //先用Base64解码  

        IvParameterSpec ivSpec = new IvParameterSpec("aa11cc22ee33gg44".getBytes());         
        
        Key key = new SecretKeySpec(keySpec.getBytes(), "AES");  
        
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");  
        
        cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);    //与加密时不同MODE:Cipher.DECRYPT_MODE  
        
        byte [] ret = cipher.doFinal(b);  
        
        return new String(ret, "utf-8");  
        
      }  
}